package org.android.SuperList;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class SuperLista extends ListActivity 
{

    private DbAdapter mDbHelper;
   	
        
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.superlista);
        mDbHelper= new DbAdapter(this);
        mDbHelper.open();
        CargarDatos();
        registerForContextMenu(getListView());
        
    }
    

	public void CargarDatos()
    {
		 
    	Cursor ListaCursor=  mDbHelper.RecuperaRegistrosTabla(DbAdapter.VISTA_LISTAS);
    	startManagingCursor(ListaCursor);    	
		String[] Origen = new String[]{DbAdapter.LISTA,DbAdapter.PRESUPUESTO,DbAdapter.GASTOS,DbAdapter.DIFERENCIA};
		int [] Destino = new int[]{R.id.txvnombrelista,R.id.txvpresupuesto,R.id.txvgastos,R.id.txvdiferencia};    	
		SimpleCursorAdapter listas = new SimpleCursorAdapter(this,R.layout.registrolista,ListaCursor,Origen,Destino);		
		setListAdapter(listas);
		//setFont("/fonts/andrewsc.ttf",R.id.txvnombrelista);
    }
	
	void setFont(String path, int res)
    {
			TextView name = (TextView)findViewById(res);  
			Typeface font = Typeface.createFromAsset(this.getAssets(), path);         
			name.setTypeface(font);		
    }
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        super.onCreateOptionsMenu(menu);
        menu.add(0,clsGeneral.MENU_INSERTAR, 0, R.string.menu_insertar);
        menu.add(0,clsGeneral.MENU_CATALOGOS, 1, R.string.menu_catalogos);
        return true;        
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) 
    {
		super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, clsGeneral.MENU_ELIMINAR, 0, R.string.menu_eliminar);
        menu.add(0, clsGeneral.MENU_EDITAR, 1, R.string.menu_editar);
        menu.add(0, clsGeneral.MENU_CANCELAR, 2, R.string.menu_cancelar);
	}

    @Override
	public boolean onMenuItemSelected(int caracteristica,MenuItem item)
    {
    	switch(item.getItemId())
    	{
    	case clsGeneral.MENU_INSERTAR:
    		creaLista();
    		return true;
    	case clsGeneral.MENU_CATALOGOS:
    		Intent i = new Intent(this, Catalogos.class);            
            startActivityForResult(i, clsGeneral.ACTIVITY_CATALOGOS);
    		return true;
    	}	
    	return super.onMenuItemSelected(caracteristica, item);
    } 
    
    public void creaLista()
    {
    	Intent i= new Intent(this,EditasListas.class);
    	startActivityForResult(i,clsGeneral.ACTIVITY_CREAR);    	
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) 
    {
    	super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, DetalleLista.class);
        i.putExtra(DbAdapter.ID, id);
        startActivityForResult(i, clsGeneral.ACTIVITY_DETALLE);
    }
  
    @Override
    protected void onActivityResult(int requestCode, int resultCode,Intent intent) 
    {
        super.onActivityResult(requestCode, resultCode, intent);
        CargarDatos();
    }
    
    @Override
	public boolean onContextItemSelected(MenuItem item) 
    {
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		switch(item.getItemId()) 
		{
    	case clsGeneral.MENU_ELIMINAR:
    		
	        mDbHelper.eliminaRegistro(DbAdapter.TABLA_LISTAS,info.id);
	        CargarDatos();	        
	        return true;
    	case clsGeneral.MENU_EDITAR:    		
            Intent i = new Intent(this, EditasListas.class);
            i.putExtra(DbAdapter.ID,info.id);
            startActivityForResult(i, clsGeneral.ACTIVITY_EDITAR);    		
    		return true;
		case clsGeneral.MENU_CANCELAR:    		
			setResult(RESULT_OK);    	    
    		return true;    		
    		
		}
		return super.onContextItemSelected(item);
	}
}